import com.anoopsoft.Role
import com.anoopsoft.User
import com.anoopsoft.UserRole

class BootStrap {
	def springSecurityService
	
    def init = { servletContext ->
		def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)
		def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
		def superAdminRole = Role.findByAuthority('ROLE_SUPERADMIN') ?: new Role(authority: 'ROLE_SUPERADMIN').save(failOnError: true)

		
		def adminUser = User.findByUsername('admin') ?: new User(
			username: 'admin',
			password: 'admin',
			enabled: true).save(failOnError: true)
			
		if (!adminUser.authorities.contains(adminRole)) {
			UserRole.create adminUser, adminRole
		}
		
		def adminUser2 = User.findByUsername('admin2') ?: new User(
			username: 'admin2',
			password: 'admin2',
			enabled: true).save(failOnError: true)
			
		if (!adminUser2.authorities.contains(adminRole)) {
			UserRole.create adminUser2, adminRole
		}
	
		
		def ordinaryUser = User.findByUsername('user') ?: new User(
			username: 'user',
			//password: springSecurityService.encodePassword('user'),
			password: 'user',
			enabled: true).save(failOnError: true)
			
		if (!ordinaryUser.authorities.contains(userRole)) {
			UserRole.create ordinaryUser, userRole
		}

		
		def superAdmin = User.findByUsername('superAdmin') ?: new User(
			username: 'superAdmin',
			password: 'superAdmin',
			enabled: true).save(failOnError: true)
			
		if (!superAdmin.authorities.contains(superAdminRole)) {
			UserRole.create superAdmin, superAdminRole
		}
		
	    }
	
    def destroy = {
    }
}
